
#include "CxBranchThread.h"

CxBranchThread::CxBranchThread(_Buffer *revbuffer,_Buffer *handlebuffer,
	_Buffer *databasebuffer,size_t number):

					m_revbuffer(revbuffer),
					m_handlebuffer(handlebuffer),
					m_databasebuffer(databasebuffer),
					m_number(number),
					m_spin(true)
{
	CX_ASSERT(m_revbuffer);	
	CX_ASSERT(m_handlebuffer);
	CX_ASSERT(m_databasebuffer);
}

CxBranchThread::~CxBranchThread()
{
	Close();
}
//»½ÐÑÌõ¼þ±äÁ¿£¬¿ªÊ¼Ïß³Ì
void		CxBranchThread::Start()
{
}
void		CxBranchThread::Close()
{
	m_spin=false;
    m_revbuffer->LoseEffictive();
    m_databasebuffer->LoseEffictive();
    m_handlebuffer->LoseEffictive();
	m_thread.interrupt();
	m_thread.join();
}

void		CxBranchThread::RunFunc()
{
	try
	{
		//¿ªÊ¼·Ö·¢ÏûÏ¢
		while(m_spin)
		{
			boost::this_thread::interruption_point();
			boost::this_thread::sleep(boost::posix_time::milliseconds(33));
			//ÏÈpopÊý¾Ý
			Mat _tmpmat=m_revbuffer->Pop();
			//ÔÙpush´ý´¦ÀíÊý¾Ý

			m_databasebuffer->Push(_tmpmat);
			m_handlebuffer->Push(_tmpmat);			
		}
	}
	catch(boost::thread_interrupted &)
	{

	}
	catch(const boost::system::system_error &e)
	{
		CX_ERR_MESSAGE(e.what())
	}
}
